package com.zjj.bams.dao;

import com.zjj.bams.entity.Account;

import java.util.Arrays;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
public class ArrayDao implements IDao<Account, Account[]>{

    //数组容器     存储所有账户信息
    private Account[] accs = new Account[3];
    //操作下标     统计账户数量
    private int index = 0;

    //初始ID
    private Long id = 100000000000000000L;

    @Override
    public Long getId() {
        return this.id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    //插入
    public boolean insert(Account acc){
        if(index>=accs.length){
            //数组扩容
            accs = Arrays.copyOf(accs , accs.length*3/2+1);
        }
        //添加元素
        accs[index++] = acc;
        return true;
    }

    //根据ID和密码查询数据
    public Account selectOne(Long id , String password){
        for (int i = 0; i < index; i++) {
            Account acc = accs[i];
            if(acc!=null && acc.getId().equals(id) && acc.getPassword().equals(password)){
                return acc;
            }
        }
        return null;
    }

    //根据ID查询数据
    public Account selectOne(Long id){
        for (int i = 0; i < index; i++) {
            Account acc = accs[i];
            if(acc!=null && acc.getId().equals(id)){
                return acc;
            }
        }
        return null;
    }

    //查询所有银行账户
    public Account[] selectAll(){
        return accs;
    }


}
